/**
 * @projectName jwdcljx
 * @package com.zcx.project.controller
 * @className com.zcx.project.controller.CommonController
 * @copyright Copyright 2024 gy, Inc All rights reserved.
 */
package com.zcx.project.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.zcx.atom.util.DateFormatter;
import com.zcx.project.mapper.ZcxPlanTaskMapper;

/**
 * CommonController
 *
 * @author gy
 * @version v1.0.0
 * @description
 * @date 2024/4/22 18:35
 */
@Controller
@RequestMapping("/common")
public class CommonController {
    @Autowired
    ZcxPlanTaskMapper zcxPlanTaskMapper;
    private static String tempfilepath = "D:/";

    @ResponseBody
    @RequestMapping(value = "/uploadFile")
    public Map<String, Object> uploadFile(@RequestParam("fj") MultipartFile file, HttpServletRequest request) throws IOException {
        Map<String, Object> result = new HashMap<>();
//
//        // 转型为MultipartHttpRequest：
//        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
//        // 获得文件：
//        MultipartFile file= multipartRequest.getFile("file");
//        MultipartFile file = files.get(0);'
        String path = tempfilepath + DateFormatter.formatDate("yyyy-MM-dd", new Date());
        String fileName = path + "/" + DateFormatter.formatDate("yyyyMMddHHmmss", new Date()) + "_zcx_" + file.getOriginalFilename();
        File tempath = new File(path);
        if (!tempath.exists()) {
            tempath.mkdir();
        }
        File temfile = new File(fileName);
        if (!temfile.exists()) {
            temfile.createNewFile();
        }
        OutputStream outStream = new FileOutputStream(temfile);
        byte[] buffer = new byte[4096];
        int bytesRead;

        InputStream inputStream = file.getInputStream();
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, bytesRead);
        }
        result.put("url", fileName);
//        zcxPlanTaskMapper.selectByPrimaryKey(id);
        return result;
    }

    @RequestMapping(value = "/downloadFile")
    public ResponseEntity<byte[]> downloadFile(String url, HttpServletResponse response) throws IOException {
        try {
            String[] s = url.split("_zcx_");
//            InputStream stream = new FileInputStream(new File(url));
//            String filename = new String(s[1].getBytes("ISO8859-1"), StandardCharsets.UTF_8);
//            response.setHeader("Content-Disposition", "attachment;filename=" + filename);
//            ServletOutputStream servletOutputStream = response.getOutputStream();
//            int len;
//            byte[] buffer = new byte[1024];
//            while ((len = stream.read(buffer)) > 0) {
//                servletOutputStream.write(buffer, 0, len);
//            }
//            servletOutputStream.flush();
//            stream.close();
//            servletOutputStream.close();
            HttpHeaders headers = new HttpHeaders();//http头信息
            String downloadFileName = new String(s[1].getBytes("UTF-8"), "UTF-8");//设置编码
            headers.setContentDispositionFormData("attachment", downloadFileName);
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(new File(url)), headers, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("下载报错异常！");
        }
    }
}